package com.xx.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ruby
 * @create 2023-11-06 10:08 AM
 */
@Slf4j
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //添加编解码，此处代码为解析tcp传过来的参数，为UTF-8格式，可以自定义解码格式
//        pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        //添加编码，此处代码为将服务端的数据返回给客户端时，编码为HEX格式 HEX格式
        pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        //hex编码
//        pipeline.addLast(new HexStringToBytesDecoder());
        pipeline.addLast(new NettyServerHandler());
    }
}

